var path = require("path");
var spawnSync = require('child_process').spawnSync;
var fs = require('fs');


function parseInt_10(a){
    return parseInt(a,10);
}
function parseFloat_10(a){
    return parseFloat(a,10);
}
function getTime(){
    return (new Date()).getTime();
}


function createJqueryObject(html,jquery){
    if(typeof html!=='string'){
        html = '';
    }
    return jquery('<div>'+html+'</div>');

}


// 获取当前分支
function getCurrentBranch(git_folder){
    if((typeof git_folder!=='string')||(!git_folder.trim())){
        return null;
    }
    var n_folder = path.normalize(git_folder);
    if(fs.existsSync(n_folder) && fs.existsSync(path.join(n_folder,'.git'))){
        var branch_output_obj = spawnSync('git',['branch'],{
            cwd:git_folder,
            encoding:'utf8'
        });
        var stdout = (branch_output_obj||{}).stdout;
        if(typeof stdout!=='string'){
            return null;
        }
        var m = stdout.match(/\*\s*(\w+)\n/);
        if(m&&m[1]){
            return m[1];
        } else {
            return null;
        }
    } else {
        return null;
    }
}

//
function getCurrentCommitHash(git_folder){
    if((typeof git_folder!=='string')||(!git_folder.trim())){
        return null;
    }
    var n_folder = path.normalize(git_folder);
    if(fs.existsSync(n_folder) && fs.existsSync(path.join(n_folder,'.git'))){
        var branch_output_obj = spawnSync('git',['log','-1'],{
            cwd:git_folder,
            encoding:'utf8'
        });
        var stdout = (branch_output_obj||{}).stdout;
        if(typeof stdout!=='string'){
            return null;
        }

        var m = stdout.match(/^commit\s+([a-zA-Z0-9_]+)\b/);
        if(m&&m[1]){
            return m[1];
        } else {
            return null;
        }
    } else {
        return null;
    }
}

/*
// todo 支持中文
// Note : 只支持英文名的
// 获取当前wifi名
// Netsh WLAN show interfaces
function getCurrentUseWifiSsid(){
    var branch_output_obj = spawnSync('Netsh',['WLAN','show','interfaces'],{
        encoding:'utf8'
    });
    var stdout = (branch_output_obj||{}).stdout;
    if(typeof stdout!=='string'){
        return null;
    }

    var m = stdout.match(/\bSSID\s*:\s*([a-zA-Z0-9_]+)\b/);
    if(m&&m[1]){
        return m[1];
    } else {
        return null;
    }
}

// 判断WiFi是否是JD
function currentWifiIsJD(){
    var current = getCurrentUseWifiSsid();
    if(typeof current!=='string'){
        return false;
    }
    return  current.toUpperCase() === 'JD';
}
*/

function atleastTwoChar(str) {
    str = str.toString();

	if(str.length===0){
		return '00';
	}
	if(str.length===1){
		return '0' + str;
	}
	return str;
}


//  获取bundle文件夹所在完整路径
function get_dirname_of_bundle_folder() {
	var bundle_js_1 = path.join(__dirname,'./bundle.js');
	if(fs.existsSync(bundle_js_1)){
		return __dirname;
	}
	var bundle_js_2 = path.join(__dirname,'../bundle.js');
	if(fs.existsSync(bundle_js_2)){
		return path.join(__dirname,'../');
	}
	var bundle_js_3 = path.join(__dirname,'../../bundle.js');
	if(fs.existsSync(bundle_js_3)){
		return path.join(__dirname,'../../');
	}
}
function get_dirname_of_comment_variables_folder(filename) {
	return path.join(get_dirname_of_bundle_folder(),'/jsbundle_split/comment_variables/'+filename);
}
function write_comment_file(filename,obj) {
	fs.writeFileSync(
		get_dirname_of_comment_variables_folder(filename),
		(typeof obj==='string')?obj:JSON.stringify(obj,null,4),
		'utf8'
	);
}

// 替换bundle之前的路径，只打印简洁路径
function replace_bundle_folder_for_print(the_path) {
	var path_of_bundle = get_dirname_of_bundle_folder();
	return the_path.replace(path_of_bundle,'');
}



function getPropValue(obj,propArr){
	if(!obj){
		return undefined;
	}
	if((!propArr) || (!propArr.push) || (!propArr.length)){
		return undefined;
	}
	var current = obj;
	var len = propArr.length;
	for(var i=0; i<len; i++){
		var key = propArr[i];
		if(!current){
			break;
		}
		current = current[key];
	}
	return current;
}

module.exports = {
    getCurrentBranch:getCurrentBranch,
	createJqueryObject:createJqueryObject,
	atleastTwoChar:atleastTwoChar,
	get_dirname_of_bundle_folder:get_dirname_of_bundle_folder,
	getPropValue:getPropValue,
	replace_bundle_folder_for_print:replace_bundle_folder_for_print,
	get_dirname_of_comment_variables_folder:get_dirname_of_comment_variables_folder,
	write_comment_file:write_comment_file
};